package com.fz.song.controller;


import com.fz.song.common.Constants;
import com.fz.song.enums.ExceptionEnum;
import com.fz.song.exception.CustomException;
import com.fz.song.pojo.SysResource;
import com.fz.song.pojo.SysRole;
import com.fz.song.pojo.SysRoleResource;
import com.fz.song.pojo.SysUser;
import com.fz.song.service.ResourceService;
import com.fz.song.service.RoleService;
import com.fz.song.service.UserService;
import com.fz.song.util.DateUtils;
import com.fz.song.util.NumberUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("rolecontroller")
public class RoleController extends BaseController {

    @Autowired

    public RoleService roleService;
    @Autowired
    public ResourceService resourceService;


    @RequestMapping(value = "roles")
    public String roles(HttpServletRequest request, HttpServletResponse response) {
        return "role/roles";
    }

    @RequestMapping("roles_list")
    public String roles_list(@RequestParam(value = "page", defaultValue = "1") int page, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = new HashMap();
        String condition = request.getParameter("condition");
        params.put("condition", condition);
        PageInfo<SysRole> datalist = roleService.queryRoleByPage(params, page, Constants.LIST_SIZE);
        request.setAttribute("datalist", datalist);
        return "role/roles_list";
    }

    @RequestMapping("roles_getPowers")
    public String roles_getPowers(HttpServletRequest request, HttpServletResponse response) {
        int sysid = NumberUtil.Str2Int(request.getParameter("sysid"), -1);
        //查询所有的资源
        List<SysResource> sysResources = resourceService.queryResource();
        //查询我现在已经拥有的资源
        List<SysRoleResource> sysRoleResources = resourceService.queryHaveResource(sysid);

        for (SysResource resource : sysResources) {
            Integer resourceId = resource.getSysid();
            for (SysRoleResource roleResource : sysRoleResources) {
                Integer roleResourceId = roleResource.getResourceid();
                if (resourceId == roleResourceId) {
                    resource.setIshave(true);
                    break;
                }
            }
            List<SysResource> childs = resource.getChilds();
            for (SysResource child : childs) {
                Integer childid = child.getSysid();
                for (SysRoleResource roleResource : sysRoleResources) {
                    Integer roleResourceId = roleResource.getResourceid();
                    if (childid == roleResourceId) {
                        child.setIshave(true);
                        break;
                    }
                }
            }
        }
        request.setAttribute("powers", sysResources);
        request.setAttribute("sysid", sysid);
        return "role/roles_powers";
    }

    @RequestMapping("roles_savePowers")
    @ResponseBody
    public Object roles_savePowers(HttpServletRequest request, HttpServletResponse response) throws CustomException {
        Integer sysid = NumberUtil.Str2Int(request.getParameter("sysid"), -1);
        String powerss = request.getParameter("powerss");
        if (sysid == -1) {
            ExceptionEnum paramException = ExceptionEnum.PARAM_EXCEPTION;
            throw new CustomException(paramException.getCode(), paramException.getMessage());
        }
        boolean flag = roleService.roles_savePowers(sysid, powerss);
        if (flag)
            return result(1, "操作成功");
        return result(0, "操作失败");
    }

    @RequestMapping("roles_saveRole")
    @ResponseBody
    public Object roles_saveRole(HttpServletRequest request, HttpServletResponse response) throws CustomException {
        String roleidStr = request.getParameter("sysid");
        String rolename = request.getParameter("rolename");
        String description = request.getParameter("description");
        int roleid = NumberUtil.Str2Int(roleidStr, -1);

        SysRole role = null;
        if (roleid == 0) {
            role = new SysRole();
            role.setCreatetime(DateUtils.getTimeNow());
        } else {
            role = roleService.getRoleById(roleid);
            role.setUpdatetime(DateUtils.getTimeNow());
        }

        role.setName(rolename);
        role.setDescription(description);

        int num = roleService.saveRole(role);

        if (num == 1)
            return result(1, "操作成功");
        else
            return result(0, "操作失败");


    }

    @RequestMapping("roles_getRole")
    @ResponseBody
    public Object roles_getRole(HttpServletRequest request, HttpServletResponse response) throws CustomException {
        String sysidstr = request.getParameter("sysid");
        int sysid = NumberUtil.Str2Int(sysidstr, -1);
        SysRole role = roleService.getRoleById(sysid);

        if (role == null)
            return result(0, "查询失败", role);
        return result(1, "查询成功", role);
    }

    @RequestMapping("roles_deleteRole")
    @ResponseBody
    public Object roles_deleteRole(HttpServletRequest request, HttpServletResponse response) throws CustomException {
        String sysidstr = request.getParameter("sysid");
        int sysid = NumberUtil.Str2Int(sysidstr, -1);
        int num = roleService.deleteRole(sysid);
        if (num == 1)
            return result(1, "删除成功");
        return result(0, "删除失败");
    }

}
